package com.example.easycharge.repository;

import com.example.easycharge.entity.MessageItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface MessageItemRepository extends JpaRepository<MessageItem, Long> {
    Page<MessageItem> findAllByCustomerId(Pageable pageable, long userId);

    Page<MessageItem> findAllByCustomerIdAndHasRead(Pageable pageable, long userId,boolean hasRead);

    @Modifying
    @Transactional
    @Query(value = "update MessageItem messageItem set messageItem.hasRead = true where messageItem.messageItemId=:messageId")
    void readMessageByMessageId(long messageId);

    @Modifying
    @Transactional
    @Query(value = "update MessageItem messageItem set messageItem.hasRead = true where messageItem.customerId=:customerId")
    void readAllByCustomerId(long customerId);

    @Modifying
    @Transactional
    int deleteMessageItemByMessageItemId(long messageId);

    int countMessageItemByMessageItemIdAndCustomerId(long messageItemId,long customerId);

    int countAllByMessageItemId(long messageItemId);
}
